package org.eduapp.trains.web.action.sysmgr;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eduapp.trains.ServiceException;
import org.eduapp.trains.crawler.TrainDataValidationController;
import org.eduapp.trains.model.train.ModelTrain;
import org.eduapp.trains.service.train.TrainService;
import org.eduapp.trains.web.action.BaseAction;

public class SysMgrAction extends BaseAction {
	
	private final static Logger logger = Logger.getLogger(SysMgrAction.class);
	
	private TrainService trainService;
	
	/**
	 * update train information
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward sysTrainInfoUpdate (ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		List<ModelTrain> models = null;
		try {
			models = trainService.getTrainToBeUpdated();
			
			String[] urlSeeds = null;
			
			String[] defaultSeeds = new String[] {
				"http://www.huochepiao.com/lieche/chengjikuaiche_liecheshikebiao.htm",
				"http://www.huochepiao.com/lieche/gaosudongche_liecheshikebiao.htm",
				"http://www.huochepiao.com/lieche/kongtiaokuaisu_liecheshikebiao.htm",
				"http://www.huochepiao.com/lieche/linke_liecheshikebiao.htm",
				"http://www.huochepiao.com/lieche/piaojiashuoming.htm",
				"http://www.huochepiao.com/lieche/pukuai_liecheshikebiao.htm",
				"http://www.huochepiao.com/lieche/zhidatekuai_liecheshikebiao.htm",
				"http://www.huochepiao.com/lieche/dongchezu_liecheshikebiao.htm"
			};
			
			int count = 0;
			if (models != null && models.size() > 0) {
				urlSeeds = new String[models.size() + defaultSeeds.length];
				for (ModelTrain model : models) {	
					urlSeeds[count++] = 
						"http://search.huochepiao.com/chaxun/resultc.asp?txtcheci=" + model.getTrainNumber();
				}
			} 
			
			if (urlSeeds == null) {
				urlSeeds = new String[defaultSeeds.length];
			}
			
			for (String urlSeed : defaultSeeds) {
				urlSeeds[count++] = urlSeed;
			}
			
			TrainDataValidationController controller = new TrainDataValidationController(urlSeeds);
			controller.startCrawl();
		} catch (ServiceException e) {
			logger.error("[eduapp > TrainService:getTrainToBeUpdated] Train fetch failed, caused by " + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			logger.error("[eduapp > SysMgrAction:sysTrainInfoUpdate] Train update failed, caused by " + e.getMessage());
			e.printStackTrace();
		}
		
		return mapping.findForward("home.index");
	}

	public static Logger getLogger() {
		return logger;
	}

	public void setTrainService(TrainService trainService) {
		this.trainService = trainService;
	}

	public TrainService getTrainService() {
		return trainService;
	}
	
}
